import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/auth.dart';
import 'package:kxy_flutter_app/components/empty_widget.dart';
import 'package:kxy_flutter_app/components/loading_widget.dart';
import 'package:kxy_flutter_app/components/refresh_header.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/model/break_rule.dart';
import 'package:kxy_flutter_app/view/my/basic_info/break_rule_detail_view.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class BreakRuleListView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return BreakRuleListViewState();
  }
}

class BreakRuleListViewState extends State<BreakRuleListView> {
  Future loadFuture;
  List<BreakRuleProfile> profileList;
  RefreshController controller = new RefreshController();

  @override
  void initState() {
    loadFuture = loadBreakRuleList();
    super.initState();
  }

  Future loadBreakRuleList() async {
    var res = await Http.instance.post(
      '/usercenter-app/breakrule/list',
      params: {'userId': Auth.instance.userId},
    );

    profileList = new List<BreakRuleProfile>();
    (res.data as List).forEach((item) {
      setState(() {
        var brp = BreakRuleProfile.fromJson(item);
        profileList.add(brp);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(title: Text('违章记录')),
      body: FutureBuilder(
        future: loadFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return body();
          }
          return LoadingWidget();
        },
      ),
    );
  }

  Widget body() {
    if (profileList.length == 0) {
      return EmptyWidget(onRefresh: loadBreakRuleList);
    }

    return SmartRefresher(
      controller: controller,
      enablePullDown: true,
      enablePullUp: false,
      header: RefreshHeader(),
      child: ListView(
        children: profileList.map((item) {
          return breakRuleItem(item);
        }).toList(),
      ),
      onRefresh: () async {
        await loadBreakRuleList();
        controller.refreshCompleted();
      },
    );
  }

  Widget breakRuleItem(BreakRuleProfile breakRule) {
    return GestureDetector(
      child: Container(
        margin: EdgeInsets.only(bottom: 1),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            bottom: BorderSide(
              color: Colors.black12,
              width: 0.5,
            ),
          ),
        ),
        padding: EdgeInsets.all(20),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    breakRule.reason,
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                      color: Colors.black,
                    ),
                    overflow: TextOverflow.ellipsis,
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 10),
                    child: Text(
                      breakRule.breakRule,
                      style: TextStyle(
                        fontSize: 13,
                        color: Colors.black,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 10),
              child: Column(
                children: [
                  Text(
                    breakRule.breakRuleTime,
                    style: TextStyle(
                      fontSize: 13,
                      color: Colors.black54,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 10),
                    child: Text(
                      '',
                      style: TextStyle(fontSize: 12),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      onTap: () => gotoDetail(breakRule.id),
    );
  }

  Future gotoDetail(String id) async {
    await Navigator.of(context).push(
      MaterialPageRoute(builder: (cxt) {
        return BreakRuleDetailView(id: id);
      }),
    );

    setState(() {
      this.loadFuture = this.loadBreakRuleList();
    });
  }
}
